package cn.edu.neusoft.food729.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.example.retroiftdemo_729.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.food729.Bean.ShopBean;
import cn.edu.neusoft.food729.activity.FoodListActivity;
import cn.edu.neusoft.food729.common.Common;

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<ShopBean> list;
    private LayoutInflater layoutInflater;

    public ShopAdapter(Context context, List<ShopBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<ShopBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = layoutInflater.inflate(R.layout.shop_item, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        ShopBean entity = list.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.res_name.setText(entity.getShopname());
        viewHolder.res_address.setText(entity.getAddress());
        viewHolder.res_bar.setRating(entity.getLevel());
        // viewHolder.image.setImageResource(Integer.parseInt(entity.getPic()));
        Picasso.with(context)
                .load(Common.BASEURL + entity.getPic())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.image);
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = list.get(position).getShop_id();
                Toast.makeText(context, "" + id, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, FoodListActivity.class);
//                intent.putExtra("resid",list.get(position).getResid());
//                intent.putExtra("idolid",list.get(position).getUserid());
                intent.putExtra("image", list.get(position).getPic());
                intent.putExtra("shop_id", list.get(position).getShop_id());
                intent.putExtra("shop_name", list.get(position).getShopname());
                intent.putExtra("shop_adress", list.get(position).getAddress());
                intent.putExtra("tel", list.get(position).getPhonenum());
                intent.putExtra("intro", list.get(position).getIntro());
                intent.putExtra("level", list.get(position).getLevel());
                context.startActivity(intent);

            }
        });
    }


    @Override
    public int getItemCount() {
        if (list != null)
            return list.size();
        else
            return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RatingBar res_bar;
        TextView res_name, res_address;
        ImageView image;
        Button button;

        public ViewHolder(View view) {
            super(view);
            res_name = view.findViewById(R.id.res_name);
            res_bar = view.findViewById(R.id.ratingBar1);
            image = view.findViewById(R.id.food_image);
            res_address = view.findViewById(R.id.food_price);
            button = view.findViewById(R.id.button6);
        }
    }
}

